
package com.jing.weibo.scheduler.task;

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jing.system.utils.DateUtil;

/**
 * 定时任务加载工具类
 * @author jing.yue
 * @version 2012/09/18 1.0.0
 */
@Component
public class TaskManager {

	// log4j日志
	private static final Logger logger = Logger.getLogger(TaskManager.class);

	@Autowired
	private Scheduler scheduler;

	@Autowired
	private GetNewMpUserTask getNewMpUserTask;


	/**
	 * 添加获取可以互粉的用户的任务
	 * @return false：失败 true:成功
	 */
	public boolean addGetNewMpUserTask() {
		boolean type = false;
		try {
			JobDetail jobDetail =new  JobDetail();
			jobDetail.setName("job_getnewmpuser" + DateUtil.getTime());
			jobDetail.getJobDataMap().put("task", getNewMpUserTask);
			jobDetail.setJobClass(SimpleJob.class);
			scheduler.addJob(jobDetail, true);
			CronTrigger cronTrigger =new CronTrigger("cron_getnewmpuser" + DateUtil.getTime(), Scheduler.DEFAULT_GROUP, jobDetail.getName(), Scheduler.DEFAULT_GROUP);
			//cronTrigger.setCronExpression(SystemCons.get_new_mp_user_task_time);
			cronTrigger.setCronExpression("");

			scheduler.scheduleJob(cronTrigger);
			type = true;
			logger.info("添加获取可以互粉的用户的Job任务成功!");
		} catch (Exception e) {
			logger.info("添加获取可以互粉的用户的Job任务错误!", e);
		}

		return type;
	}

	/**
	 * 删除发微博任务
	 * @param username 用户名
	 * @param token
	 * @param fansAttentionType 同步类型
	 * @return false：失败 true:成功
	 */
	/*public boolean cancelTask(String username, String token, FansAttentionType fansAttentionType) {
		boolean type = false;
		try {
			scheduler.unscheduleJob(CRON + username + fansAttentionType + token, Scheduler.DEFAULT_GROUP);
			type = true;
		} catch (SchedulerException e) {
			logger.info(username + ":删除[类型: " + fansAttentionType.getText() + "]任务错误!", e);
		}
		return type;
	}*/

}
